package com.github.lee.core.common.ext

import android.content.Intent
import android.os.Parcelable
import androidx.annotation.NonNull
import androidx.annotation.Nullable
import androidx.fragment.app.Fragment
import com.github.lee.core.common.LogUtil
import com.orhanobut.logger.Logger

fun Fragment.log(msg: Any) {
    LogUtil.log(msg, Logger.ERROR)
}


fun <T : Parcelable?> Fragment.getBundleParcelable(
    @Nullable intent: Intent?,
    @NonNull key: String
): T? {
    if (null == intent) {
        return null
    }
    val bundle = intent.extras ?: return null
    return bundle.getParcelable<T>(key)
}

fun <T : Parcelable?> Fragment.getArgumentParcelable(
    @NonNull key: String
): T? {
    val bundle = arguments ?: return null
    return bundle.getParcelable<T>(key)
}

fun <T : Parcelable> Fragment.getArgumentParcelableList(
    @NonNull key: String
): List<T> {
    val bundle = arguments ?: return emptyList()
    return bundle.getParcelableArrayList<T>(key)?.toList() ?: emptyList()
}

@Suppress("UNCHECKED_CAST")
inline fun <reified T> Fragment.getArgumentValue(key: String, defaultValue: T? = null): T? {
    val bundle = arguments ?: return defaultValue
    val value = bundle.get(key) ?: return defaultValue
    return value as T
}
//
//fun <T> Activity.getBundleValue(key: String, defaultValue: T? = null): T? {
//    return getBundleValue(intent, key, defaultValue)
//}
